package cn.team11.sds.dayi.ble;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import cn.team11.sds.dayi.R;

import android.Manifest;
import android.app.Activity;
import android.bluetooth.BluetoothDevice;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.os.Build;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import com.aliyun.iot.smurfs.Smurfs;
import com.aliyun.iot.smurfs.base.PairScene;
import com.aliyun.iot.smurfs.listener.PairSceneListener;



/*


Smurfs PairScene 使用

        //创建 params(ctx,delegate)需要实现PairSceneListener接口
        this.scene = Smurfs.creatPairScene(this,this);
        //参数1:是否过滤非alink协议的蓝牙设备 2：是否过滤重复广播设备
        this.scene.setfilterSetting(true,true);

        //启动
        scene.enter();

        //关闭扫描
        scene.exit()
 */


public class BLEDiscoveryActivity extends Activity implements PairSceneListener {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        configListview();
        configBLE();
    }


    ListView listView;
    List<Map<String, Object>> data;
    SimpleAdapter adapter;
    PairScene scene;


    @Override
    public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
        if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {
            super.onRequestPermissionsResult(requestCode, permissions, grantResults);
            this.scene.enter();
        }
    }

    private void configBLE(){

        //初始化需要传入context
        Smurfs.preload(getApplicationContext());
        this.scene = Smurfs.creatPairScene(this,this);
        this.scene.setfilterSetting(false,true);

        //android6.0+系统,需要申请权限
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M &&
            (this.checkSelfPermission(Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED)){
            requestPermissions(new String[]{Manifest.permission.ACCESS_COARSE_LOCATION},1);
        }else {
            this.scene.enter();
        }

    }

    private void configListview(){
        this.data  = new ArrayList<Map<String, Object>>();

        //        Map<String, Object> map = new HashMap<String, Object>();
        //        map.put("peripheralName", "G1");
        //        map.put("info", "google 1");
        //        this.data.add(map);

        adapter = new SimpleAdapter(this,this.data,R.layout.blediscovery_listview,
            new String[]{"mac","info"},
            new int[]{R.id.mac,R.id.info});
        //        setListAdapter(adapter);

        listView = new ListView(this);
        listView.setAdapter(adapter);
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

                scene.exit();
                Intent intent = new Intent();
                String mac = data.get(position).get("mac").toString();
                intent.putExtra("mac",mac);
                intent.setClass(BLEDiscoveryActivity.this, BLEOperateActivity.class);
                startActivity(intent);

            }
        });
        setContentView(listView);
    }


    @Override
    public void onDiscoverPeripheral(BluetoothDevice device, int rssi, byte[] scanRecord, String mac) {
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("mac",mac);
        map.put("info", "name:"+  device.getName() + " | rssi:" + rssi);
        this.data.add(map);
        adapter.notifyDataSetChanged();
    }
}
